package com.skyworth.ui.api;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by lu on 2018/1/22.
 */

public class SkySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    protected static final int FPS = 60;
    protected static final float INTERVAL = 1000 / FPS;
    private HandlerThread drawThread = null;
    private Handler handler = null;

    public SkySurfaceView(Context context) {
        super(context);
        init();
    }

    public SkySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SkySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setZOrderOnTop(true);
        getHolder().addCallback(this);
        getHolder().setFormat(PixelFormat.TRANSPARENT);
    }

    @Override
    public final void surfaceCreated(SurfaceHolder holder) {
        synchronized (this) {
            if (drawThread == null) {
                drawThread = new HandlerThread("DrawThread", Thread.MAX_PRIORITY);
                drawThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        e.printStackTrace();
                    }
                });
                drawThread.start();
            }
            if (handler == null)
                handler = new Handler(drawThread.getLooper());
        }
        _draw(new DrawHandler() {
            @Override
            public void doDraw(Context context, Canvas canvas) {
                onCreate(canvas);
            }
        });
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public final void surfaceDestroyed(SurfaceHolder holder) {
        _draw(new DrawHandler() {
            @Override
            public void doDraw(Context context, Canvas canvas) {
                onDestroy(canvas);
            }
        });
        synchronized (this) {
            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
                handler = null;
            }
            if (drawThread != null) {
                drawThread.quit();
                drawThread = null;
            }
        }
    }

    public interface DrawHandler {
        void doDraw(Context context, Canvas canvas);
    }

    protected final void draw(final DrawHandler drawHandler, long delay) {
        synchronized (this) {
            if (handler != null) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        _draw(drawHandler);
                    }
                }, delay);
            }
        }
    }

    protected final void draw(final DrawHandler drawHandler) {
        draw(drawHandler, 0);
    }

    private void _draw(DrawHandler drawHandler) {
        Canvas mCanvas = getHolder().lockCanvas();
        if (mCanvas != null) {
            try {
                drawHandler.doDraw(getContext(), mCanvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
            getHolder().unlockCanvasAndPost(mCanvas);
        }
    }

    protected void onCreate(Canvas canvas) {

    }

    protected void onDestroy(Canvas canvas) {

    }
}
